/*
 * CopyScriptEntry.java
 *
 * Created on November 1, 2007, 10:08 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package filecopier.script;

import java.io.File;

/**
 *
 * @author david
 */
public class CopyScriptEntry extends AbstractScriptEntry {

    private File path = null;
    private ScriptEntry.PathType pathType = null;

    /**
     * Creates a new instance of CopyScriptEntry
     */
    public CopyScriptEntry(ScriptEntry.PathType pathType, File path) {
        this.pathType = pathType;
        this.path = path;
    }

    protected CopyScriptEntry(ScriptEntry.PathType pathType, ScriptEntry.SavedState savedState, File path) {
        this(pathType, path);
        this.savedState = savedState;
    }

    public File getPath() {
        return path;
    }

    public File getPath(ScriptEntry.PathType pathType) {
        if (pathType == null) {
            throw new IllegalArgumentException("getPath() received a null for the PathType as its parameter.");
        }
        return (pathType == this.pathType ? path : null);
    }

    public ScriptEntry.PathType getPathType() {
        return pathType;
    }

    public ScriptEntry.PathType getPathType(File path) {
        if (path == null) {
            throw new IllegalArgumentException("getPathType() received a null for the File as its parameter.");
        }
        return (path.toString().equals(this.path.toString()) ? pathType : null);
    }

    // When comparing equality, don't compare the value of the saved state because this method will be called
    // for instance, when determining if a CopyScriptEntry object's saved state should be set to 'Remove'.
    @Override
    public boolean equals(Object anObject) {
        boolean equal = false;
        if (anObject != null) {
            if (this == anObject) {
                equal = true;
            } else if (anObject instanceof CopyScriptEntry) {
                CopyScriptEntry cse = (CopyScriptEntry) anObject;
                equal = (this.path.equals(cse.path) && this.pathType == cse.pathType);
            }
        }
        return equal;
    }

    public void setPath(File path) {
        if (path == null) {
            throw new IllegalArgumentException("CopyScriptEntry.setPath() received a null for the File as its parameter.");
        }
        if ((this.path == null) || (this.path != null && !this.path.equals(path))) {
            this.path = path;
            savedState = ScriptEntry.SavedState.Added;
        }
    }

    public void setPath(ScriptEntry.PathType pathType, File path) {
        setPath(path);
        setPathType(pathType);
    }

    public void setPathType(ScriptEntry.PathType pathType) {
        if (pathType == null) {
            throw new IllegalArgumentException("CopyScriptEntry.setPathType() received a null for the ScriptEntry.PathType as its parameter.");
        }
        if (pathType == ScriptEntry.PathType.Restore || pathType == ScriptEntry.PathType.Delete) {
            throw new UnsupportedOperationException("Copy scripts cannot have their file paths set to the type of " + pathType.toString());
        }
        if ((this.pathType == null) || (this.pathType != null && !this.pathType.equals(pathType))) {
            this.pathType = pathType;
            savedState = ScriptEntry.SavedState.Added;
        }
    }
}

